<template>
  <div class="top">
    <div class="left">
      <div class="lbtn">
        <span @click="goHome">首页</span>
      </div>
    </div>
    <div class="center">
      <div class="title">
        <span>智慧旅游平台可视化大数据平台</span>
      </div>
    </div>
    <div class="rigth">
      <div class="rbtn">
        <span>统计报告</span>
      </div>
      <div class="time">
        <span>当前时间:{{ OnTime }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
//引入组合式api
import { ref, onMounted, onBeforeUnmount } from "vue";
import moment from "moment";
//引入路由
import { useRouter } from "vue-router";
//定义一个变量接收时间(通过插件完成)
let OnTime = ref(moment().format("YYYY-MM-DD HH:mm:ss"));
//获取路由器对象
let $router = useRouter();
//清除定时器
let timer = ref(0);
//挂载时获取时间
onMounted(() => {
  timer.value = setInterval(getNowTime, 1000);
});
//点击跳转到首页的回调函数
const goHome = () => {
  $router.push("/home");
};
//定义一个函数，用于获取当前时间（手写函数完成）
const getNowTime = () => {
  let date = new Date();
  let year = date.getFullYear();
  let month =
    date.getMonth() + 1 >= 10
      ? date.getMonth() + 1
      : "0" + (date.getMonth() + 1);
  let day = date.getDate() >= 10 ? date.getDate() : "0" + date.getDate();
  let hour = date.getHours() >= 10 ? date.getHours() : "0" + date.getHours();
  let minute =
    date.getMinutes() >= 10 ? date.getMinutes() : "0" + date.getMinutes();
  let second =
    date.getSeconds() >= 10 ? date.getSeconds() : "0" + date.getSeconds();
  OnTime.value =
    year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
};
//组件销毁之前清除定时器
onBeforeUnmount(() => {
  clearInterval(timer.value);
});
</script>

<style scoped lang="scss">
* {
  margin: 0;
  padding: 0;
}
.top {
  display: flex;
  width: 100%;
  height: 40px;
  .left {
    flex: 1;
    background: url(../../images/dataScreen-header-left-bg.png) no-repeat;
    background-size: 100% 100%;
    .lbtn {
      float: right;
      width: 150px;
      height: 40px;
      background: url(../../images/dataScreen-header-btn-bg-l.png) no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 40px;
      font-size: 20px;
      color: #29fcff;
    }
  }
  .rigth {
    flex: 1;
    background: url(../../images/dataScreen-header-right-bg.png) no-repeat;
    background-size: 100% 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .rbtn {
      width: 150px;
      height: 40px;
      background: url(../../images/dataScreen-header-btn-bg-r.png) no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 40px;
      font-size: 20px;
      color: #29fcff;
    }
    .time {
      color: #29fcff;
      margin-right: 10px;
    }
  }
  .center {
    flex: 2;
    .title {
      width: 100%;
      height: 74px;
      background: url(../../images/dataScreen-header-center-bg.png) no-repeat;
      background-size: 100% 100%;
      text-align: center;
      line-height: 74px;
      font-size: 37px;
      color: #29fcff;
    }
  }
}
</style>
